package com.bawei.module_goods.adapter

import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_goods.R

class OptionAdapter(
    private val options: List<String>,
    private var selected: String?,
    private val onSelect: (String) -> Unit
) : RecyclerView.Adapter<OptionAdapter.OptionViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OptionViewHolder {
        val tv = TextView(parent.context)
        val params = ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        params.setMargins(0, 16, 0, 16) // 上下间距16px（约8dp）
        tv.layoutParams = params
        tv.setPadding(24, 12, 24, 12)
        tv.textSize = 16f
        return OptionViewHolder(tv)
    }

    override fun getItemCount() = options.size

    override fun onBindViewHolder(holder: OptionViewHolder, position: Int) {
        val value = options[position]
        val tv = holder.textView
        tv.text = value
        tv.setBackgroundResource(if (value == selected) R.drawable.bg_option_selected else R.drawable.bg_option_unselected)
        tv.setTextColor(
            if (value == selected) tv.context.getColor(R.color.colorPrimary)
            else tv.context.getColor(R.color.black)
        )
        tv.setOnClickListener {
            val old = selected
            selected = value
            notifyItemChanged(options.indexOf(old))
            notifyItemChanged(position)
            onSelect(value)
        }
    }
    class OptionViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
} 